/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset

* Sample
keep if sample_int==1 & eis_f!=. // keep observations of interest


* ---------
* Table A19
* ---------

* Globals
global dep "ln_time fraud c1_dlog_val c1_dlog_tax htrl_f_int"
global controls "tax_rate risk red_i mixed_dec differentiated valitrade_advice"

* Matrix
mat tableA19 = J(14,5,.)
mat tableA19_T = J(1,5,.)

* Loop for estimations
local i = 1
local j = 1
foreach x in $dep {
	
	qui: reghdfe `x' eis_f taxeis_f $controls countryorig* max_twodigit* if sample_reg==1 , absorb(insp_f key_dec ym_*) vce(cluster insp_f key_dec)
	
	mat tableA19[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat tableA19[`i',`j'] = _se[eis_f]
	local i = `i' + 1
	mat tableA19[`i',`j'] = _b[tax_rate]
	local i = `i' + 1
	mat tableA19[`i',`j'] = _se[tax_rate]
	local i = `i' + 1
	mat tableA19[`i',`j'] = _b[taxeis_f]
	local i = `i' + 1
	mat tableA19[`i',`j'] = _se[taxeis_f]
	mat tableA19_T[1,`j'] = e(N)
	local i = `i' + 8
	mat tableA19[`i',`j'] = e(r2)
	local i = `i' -13
	local j = `j' +1
}


* Export
* ------
putexcel set "$tables\Table A19.xlsx", replace sheet(TableA19) // create a new excel spreadsheet

* Coefficients
putexcel B4 = matrix(tableA19), nformat(0.000) hcenter vcenter
putexcel B16 = matrix(tableA19_T), nformat(number_sep) hcenter vcenter

* Title
putexcel (A1:F1), merge hcenter vcenter
putexcel A1 = "Before delegated randomization of inspector assignment"

* Column names
putexcel A2 = "Dependent variable:", left vcenter bold
local cells `" "B" "C" "D" "E" "F" "'
local labels `" "Time" "Fraud" "\Delta log value" "\Delta log tax" "Hyp. tax revenue losses" "'
forvalues i = 1(1)5 {
	
	local x: word `i' of `labels'
	local y: word `i' of `cells'
	
	putexcel `y'2 = "`x'", hcenter vcenter bold	
	putexcel `y'3 = "(`i')", hcenter vcenter
	
	forvalues j = 10(1)15 {
		putexcel `y'`j' = "Yes", hcenter vcenter
	}
}

* Row names
local j = 4
local names `" "Excess interaction share" "Tax rate" "Excess interaction share*Tax rate" "'
forvalues i = 1(1)3 {
	
	local x: word `i' of `names'
	
	putexcel A`j' = "`x'", left vcenter
	local j = `j'+2
}

local j = 10
local names `" "Declaration characteristics" "Inspector fixed effects" "Broker fixed effects" "Source country fixed effects" "HS2-product fixed effects" "Month-year fixed effects" "'
forvalues i = 1(1)6 {
	
	local x: word `i' of `names'
	
	putexcel A`j' = "`x'", left vcenter
	local j = `j'+1
}
putexcel A16 = "Observations", left vcenter
putexcel A17 = "R-squared", left vcenter


* -------------------------------- ENDS HERE -------------------------------- *